<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item><i class="el-icon-date"></i> 菜谱管理</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="search">
      <el-input  placeholder="按照菜名或种类搜索" v-model="keyword"></el-input>
      <el-button @click="qryMenuByName()">搜索</el-button>
    </div>
    <hr>
    <div class="cantainer">
      <el-table style="width: 100%;"
                :data="menuList.slice((currentPage-1)*pagesize,currentPage*pagesize)"
      >
        <el-table-column type="index" width="50">
        </el-table-column>
        <el-table-column label="菜名" prop="foodname" width="250">
        </el-table-column>
        <el-table-column label="价格" prop="price" width="100" >
        </el-table-column>
        <el-table-column label="种类" prop="foodtype" width="100">
        </el-table-column>
        <el-table-column label="操作" width="380">
          <template slot-scope="scope">
<!--            <el-button-->
<!--              @click="handleClick(scope.row.foodid)">点菜-->
<!--            </el-button>-->
            <el-button
              @click="handleUpdate(scope.row.foodid)">修改
            </el-button>
            <el-button
              @click="handleDelete(scope.row.foodid)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 40]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="menuList.length">
      </el-pagination>
    </div>


    <el-dialog class="dialog" title="修改" :visible.sync="dialogFormVisible" @close='closeDialog'>
      <el-form ref="form" label-width="80px">
        <el-form-item label="菜品">
          <el-input v-model="dialogData.foodname"></el-input>
        </el-form-item>

        <el-form-item label="价格">
          <el-input v-model="dialogData.price" onkeyup="this.value=this.value.replace(/\D/g,'')"></el-input>
        </el-form-item>

        <el-form-item >
          <el-button type="primary" @click="submitUpdate">立即修改</el-button>
        </el-form-item>
      </el-form>
      <!--{{dialogData}}-->
    </el-dialog>


  </div>
</template>

<script>
import {qryInfoMenu,qryMenuInfoById,clickById,updateMenuInfoById,deleteMenuInfoById,qryMenuByName} from "@/network/infomenu";

var token = localStorage.getItem('Authorization'); // 要保证取到

export default {
  name: "All",
  data() {
    return {
      currentPage: 1, //初始页
      pagesize: 10,    //    每页的数据
      menuList: [],
      dialogFormVisible: false,
      myHeaders: {Authorization: token},
      dialogData: {},
      keyword: ''
    }
  },
  created() {
    this.handleUserList()
  },
  methods: {
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function (size) {
      this.pagesize = size;
      console.log(this.pagesize)  //每页下拉显示数据
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage;
      console.log(this.currentPage)  //点击第几页
    },
    handleUserList() {
      qryInfoMenu().then(res => {
        this.menuList = res.data;
        console.log(res.data);
      });
    },
    handleUpdate(index) {
      console.log(index);
      this.dialogFormVisible = true;
      qryMenuInfoById(index).then(res => {
        this.dialogData = res.data
        console.log(res)
      });
    },
    qryMenuByName(){
      qryMenuByName(this.keyword).then(res => {
        this.menuList = res.data;
        console.log(res.data);
      });

    },
    handleDelete(index) {
      this.$confirm('此操作将永久删除该菜品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMenuInfoById(index).then(res => {
          console.log(res);
          if(res.code==1001){
            this.$message.error('删除失败！');
          }else{
            this.$message.success('删除成功！');
            this.handleUserList() ;
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    handleClick(index){
      this.$confirm('是否点该菜品?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        clickById(index).then(res => {
          console.log(res);
          if(res.code==1001){
            this.$message.error('点餐失败！');
          }else{
            this.$message.success('点餐成功！');
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消点菜'
        });
      });
    },
    submitUpdate(){
      updateMenuInfoById(this.dialogData).then(res => {
        this.$message('修改成功');
        this.$router('/form/childcomps/menu')
      });
    },
    closeDialog() {
      this.handleUserList()
    }
  }
}
</script>

<style scoped>
.right {
  display: flex;
  justify-content: flex-end ;
}

.formdiv {
  width: 400px;
  margin: 0 auto;
  text-align: center;
}

.search {
  width: 400px;
  margin: 0 auto;
  text-align: center;
}

</style>
